<template>
  <div>
    <pro-table
      ref="tableRef"
      :data="tableData"
      :total="total"
      :columns="columns"
      :tool-button="false"
      @search="commEvent.onSearch"
      :page-change="commEvent.onPageChange"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <template #tableHeader>
        <el-row justify="end">
          <el-button type="primary" icon="Plus" @click="commEvent.onSave">新增</el-button>
        </el-row>
      </template>
      <template #operation="{ row }">
        <el-button type="danger" link @click="commEvent.onDelete(row)">删除</el-button>
      </template>
    </pro-table>
    <!-- 新增点位 -->
    <save-meter-point
      v-model="saveVisible"
      :meter-id="Number(route.query.meterId)"
      :units="unitEnum"
      @refresh="commEvent.onSearch"
    />
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue"
import ProTable from "@/components/ProTable/index.vue"
import SaveMeterPoint from "./components/SaveMeterPoint.vue"
import { ColumnProps, EnumProps, ProTableInstance } from "@/components/ProTable/interface"
import { useRoute } from "vue-router"
import { ElMessage, ElMessageBox } from "element-plus"
import { dataDictionaryByType } from "@/api/modules/system"
import { meterPointPage, deleteMeterPoint } from "@/api/modules/archive"
import { SystemSpace } from "@/api/interface/archive"
// 参数
const route = useRoute()
const tableRef = ref<ProTableInstance>() // 表格实例
const tableData = ref<SystemSpace.MeterPointPage[]>([]) // 表格数据
const total = ref<number>(0) // 数据总数
const saveVisible = ref<boolean>(false) // 新增弹窗
const unitEnum = ref<EnumProps[]>([]) //
// 页码参数
const pageParams = ref({
  pageIndex: 1,
  pageSize: 10,
  meterId: Number(route.query.meterId)
})
// 表格列配置
const columns: ColumnProps<SystemSpace.MeterPointPage>[] = [
  { prop: "name", label: "点位名称", search: { el: "input" } },
  { prop: "mark", label: "点位标识符", search: { el: "input" } },
  { prop: "unit", label: "参数单位" },
  { prop: "createBy", label: "创建人" },
  { prop: "createTime", label: "创建时间", minWidth: 180 },
  { prop: "operation", label: "操作", width: 120, fixed: "right" }
]
// 事件
const commEvent = {
  // 获取参数单位
  getUnits: async () => {
    const { data } = await dataDictionaryByType("param_unit")
    unitEnum.value = data.map(item => ({
      label: item.dictLabel,
      value: item.dictValue
    }))
  },
  // 获取表格数据
  getTableData: async (params: SystemSpace.MeterPointPageQuery) => {
    const { data } = await meterPointPage(params)
    tableData.value = data.result
    total.value = data.totalCount
  },
  // 查询
  onSearch: () => {
    tableRef.value?.search()
    pageParams.value.pageIndex = 1
    const _search = tableRef.value?.searchParam
    commEvent.getTableData({ ...pageParams.value, ..._search })
  },
  // 分页
  onPageChange: (page: number, size: number) => {
    pageParams.value.pageIndex = page
    pageParams.value.pageSize = size
    const _search = tableRef.value?.searchParam
    commEvent.getTableData({ ...pageParams.value, ..._search })
  },
  // 新增
  onSave: () => {
    saveVisible.value = true
  },
  // 删除
  onDelete: (row: SystemSpace.MeterPointPage) => {
    ElMessageBox.confirm("确定删除此点位?", "删除", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning"
    })
      .then(async () => {
        const { code } = await deleteMeterPoint({ id: row.id })
        if (code !== "200") return
        ElMessage.success("删除成功")
        commEvent.onSearch()
      })
      .catch(() => {})
  }
}
onMounted(() => {
  commEvent.getTableData(pageParams.value)
  commEvent.getUnits()
})
</script>

<style lang="scss" scoped></style>
